package com.test.connect;

import java.time.Duration;
import java.util.concurrent.CountDownLatch;

import com.test.Constant;
import com.test.PropertiesInnerUtil;

import dev.miku.r2dbc.mysql.MySqlConnectionConfiguration;
import dev.miku.r2dbc.mysql.MySqlConnectionFactory;
import io.r2dbc.spi.Connection;
import io.r2dbc.spi.ConnectionFactory;
import reactor.core.publisher.Mono;

public class InsertDemo {

	private static String userName = PropertiesInnerUtil.getString(Constant.DATASOURCE_LOCATION, "datasource.username");
	private static String password = PropertiesInnerUtil.getString(Constant.DATASOURCE_LOCATION, "datasource.password");

	public static void main(String[] args) throws InterruptedException {
		CountDownLatch down = new CountDownLatch(1);
		MySqlConnectionConfiguration.Builder builder = MySqlConnectionConfiguration.builder()
	            .host("10.16.9.34")
	            .port(3306)
	            .connectTimeout(Duration.ofSeconds(3000))
	            .username(userName)
	            .password(password)
	            .database("sms_crm_db");

		MySqlConnectionConfiguration configuration = builder.build();
		ConnectionFactory connectionFactory = MySqlConnectionFactory.from(configuration);
		Mono<Connection> connectionMono = Mono.from(connectionFactory.create());

		connectionMono.flatMap(connection -> {
			return Mono.from(connection.createStatement("INSERT INTO strategy(create_user_code, create_time, modify_user_code, last_modify_time) VALUES ('who', 1, 'how', 1)").execute());
		}).subscribe(result -> {
			System.out.println(Thread.currentThread().getName());
			System.out.println(result);
			down.countDown();
		});
		down.await();
	}

}
